<template>
	<view class="cart">
		<cart-navbar :activeIndex="navActiveIndex" @changeActive="changeNavIndex"></cart-navbar>

		<cart-list @removeBtn="removeBtn" @changeState="changeState" :cartData="cartListData" @cancelBtn="cancelBtn"
			@drawBtn="drawBtn" @cancelBtnByZq="cancelBtnByZq"></cart-list>

		<view class="not-cart" v-if="cartListData.length == 0">
			<image mode="widthFix" :src="require('@/static/img/common/null_address.svg')"></image>
			<view>暂无数据</view>
			<view class="goto-login" v-if="!isVisitorLoginData" @click="gotoLogin"><text>暂无登录，去登录</text></view>
		</view>
	</view>
</template>

<script>
	import cartNavbar from './cart-child/cart-navbar.vue'
	import cartList from './cart-child/cart-list.vue'

	import {
		queryByOrder,
		cancelOrder,
		changeState,
		removeOrderByOrderId
	} from '@/api/http/main/release/dynamic.js'

	import {
		addBalance
	} from '@/api/http/main/my/pay.js'
	
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex'
	import websocketMix from '@/mixins/websocket_init.js'

	export default {
		components: {
			cartNavbar,
			cartList
		},
		data() {
			return {
				activeIndex: 1,
				navActiveIndex: 1,
				cartListData: [],
				bgColor: '#23D754',
				isVisitorLoginData: ''
			}
		},
		mixins: [websocketMix],
		computed: {
			...mapState('m_user_info', ['userInfo', 'isVisitorLogin']),
		},
		methods: {
			gotoLogin() {
				uni.navigateTo({
					url: '/pages/login/login/login'
				})
			},
			changeTopIndex(index) {
				this.activeIndex = index
				//console.log(index)
				if (index == 2) {
					this.cartListData = []
				} else {
					this.getList('0')
				}

			},
			changeNavIndex(index) {
				this.navActiveIndex = index
				//console.log(index)
				if (!this.isVisitorLoginData) {
					return
				}
				//更改查询数据
				this.getListByState(index)
			},
			getListByState(index) {
				if (index == 1) {
					//查询全部
					this.getList('0')
				} else if (index == 5) {
					//查询已签收或是 已完成
					this.getList('5,9,10')
				} else if (index == 2) {
					this.getList('2,6')
				} else if (index == 3) {
					this.getList('3,7')
				} else if (index == 4) {
					this.getList('4,8')
				} else {
					console.log(index)
				}
			},
			//获取购物车数据
			getList(state) {
				//console.log(123)
				var res = {
					typeId: '0',
					state: state
				}
				queryByOrder(res).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					this.cartListData = res.data.busInfo
				})
			},
			//取消订单
			cancelBtn(item) {
				var that = this
				//修改状态
				cancelOrder(item.id).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					//添加余额
					//price, type, method, orderId, accountsObject
					addBalance(item.price, '取消订单', '4', item.id, item.releaseUserId).then(result => {
						if (result.data.pubInfo.code !== 0) {
							return uni.$showMsg(result.data.pubInfo.message);
						}
						uni.$showMsg('取消成功');
						this.getListByState(this.navActiveIndex)
					})
				})
			},
			//赚钱取消顶大
			cancelBtnByZq(item) {
				//修改状态
				cancelOrder(item.id).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					uni.$showMsg('取消成功');
					this.getListByState(this.navActiveIndex)
				})
			},
			changeState(item, state) {
				changeState(item.id, state).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					//this.navActiveIndex = 1
					this.getListByState(this.navActiveIndex)
				})
			},

			removeBtn(item) {
				removeOrderByOrderId(item.id).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					//this.navActiveIndex = 1
					uni.$showMsg('删除订单成功！');
					this.getListByState(this.navActiveIndex)
				})
			},
			//领取现金
			drawBtn(item) {
				var that = this
				changeState(item.id, '10').then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					//给自己添加余额
					addBalance(item.price, '订单完成', '4', item.id, item.releaseUserId).then(result => {
						if (result.data.pubInfo.code !== 0) {
							return uni.$showMsg(result.data.pubInfo.message);
						}
						that.getListByState(that.navActiveIndex)
					})
				})
			}
		},
		onLoad() {
			//console.log(uni.getStorageSync('isVisitorLogin'))
			//this.$isLoginByVisitor()
			// if (uni.getStorageSync('isVisitorLogin')) {
			// 	return
			// }
			/* if (!uni.getStorageSync('isVisitorLogin')) {
				return
			}

			this.getList('0') */
		},
		onShow() {
			this.isVisitorLoginData = this.isVisitorLogin
			//this.$isLoginByVisitor()
			if (!this.isVisitorLogin) {
				return
			}
			this.getList('0')
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F8F8F8;
	}

	// .cart {
	// 	// min-height: 100vh;
	// 	background-color: #F8F8F8;
	// }


	.not-cart {
		width: 100%;
		text-align: center;

		image {
			width: 80%;
		}

		view {
			text-align: center;
			font-size: 32rpx;
			color: rgba(128, 128, 128, 1);
		}
	}

	.goto-login {
		width: 50%;
		height: 80rpx;
		background-color: #05C160;
		margin-left: 25%;
		margin-top: 50rpx;
		border-radius: 40rpx;
		text-align: center;
		line-height: 80rpx;

		text {
			color: #fff;
			font-size: 28rpx;
		}

	}
</style>